package cn.edu.nju.ws.sview.wyge.relations.adataset.overview.dataGet;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

import cn.edu.nju.ws.sview.database.DBConnectionFactory;

public class conceptHierarchy {
	public static void getConcepts(ArrayList<concept> originConcept)
			throws ClassNotFoundException, SQLException {
		ArrayList<concept> newconcept = new ArrayList<concept>();
		Connection con = DBConnectionFactory.getConnection();//.getFalconConnection();
		int n = originConcept.size();
		int m = n * n;
		int head[] = new int[originConcept.size()];
		int degree[] = new int[originConcept.size()];
		int[] to = new int[m];
		int[] next = new int[m];
		for (int i = 0; i < originConcept.size(); ++i) {
			head[i] = -1;
			degree[i] = 0;
		}
		for (int i = 0, k = 0; i < n; ++i)
			for (int j = 0; j < n; ++j) {
//				String query = "SELECT count(*) FROM sub_class_of where sub_class_uri_id = "
//						+ originConcept.get(i).id
//						+ " and super_class_uri_id = "
//						+ originConcept.get(j).id;
				String query = "SELECT count(*) FROM sub_class_of where subclass = "
						+ originConcept.get(i).id
						+ " and superclass = "
						+ originConcept.get(j).id;
				// System.out.println(query);
				Statement st = con.createStatement();
				ResultSet rs = st.executeQuery(query);
				rs.next();
				if (rs.getInt(1) == 1) {
					// System.out.println(originConcept.get(i).label + " " +
					// originConcept.get(j).label);
					next[k] = head[j];
					head[j] = k;
					to[k] = i;
					++k;
					++degree[i];
				}
				rs.close();
				st.close();
			}
		Queue<Integer> q = new LinkedList<Integer>();
		for (int i = 0; i < originConcept.size(); ++i)
			if (degree[i] == 0)
				q.add(i);
		int f[] = new int[originConcept.size()];
		int g[] = new int[originConcept.size()];
		for (int index = 0; !q.isEmpty(); ++index) {
			int i = q.remove();
			f[i] = index;
			g[index] = i;
			for (int j = head[i]; j != -1; j = next[j]) {
				--degree[to[j]];
				if (degree[to[j]] == 0)
					q.add(to[j]);
			}
			// System.out.println(i + " " + index);
		}
		for (int i = 0; i < originConcept.size(); ++i) {
			newconcept.add(originConcept.get(g[i]));
		}
		for (int i = 0; i < newconcept.size(); ++i)
			for (int j = head[g[i]]; j != -1; j = next[j]) {
				newconcept.get(f[to[j]]).father.add(i);
				newconcept.get(f[to[j]]).ancestor.add(i);
				Object ob[] = newconcept.get(i).ancestor.toArray();
				for (int k = 0; k < ob.length; ++k)
					newconcept.get(f[to[j]]).father.add((Integer) ob[k]);
			}
		/*
		 * for (int i = 0; i < newconcept.size(); ++i){ System.out.println(i +
		 * " " + newconcept.get(i).label); Object ob[] =
		 * newconcept.get(i).father.toArray(); System.out.print("father:    ");
		 * for (int j = 0; j < ob.length; ++j)
		 * System.out.print(newconcept.get((Integer)ob[j]).label + " ");
		 * System.out.println(); ob = newconcept.get(i).ancestor.toArray();
		 * System.out.print("ancestor:    "); for (int j = 0; j < ob.length;
		 * ++j) System.out.print(newconcept.get((Integer)ob[j]).label + " ");
		 * System.out.println(); }
		 */
		con.close();
		originConcept.clear();
		for (int i = 0; i < newconcept.size(); ++i) {
			originConcept.add(newconcept.get(i));
		}
	}
}
